package com.compomics.prioritizer.db.accessors;

import java.util.HashMap;
import java.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Created by IntelliJ IDEA.
 * User: Niklaas Colaert
 * Date: 7-nov-2008
 * Time: 9:15:56
 * To change this template use File | Settings | File Templates.
 */
public class Protocol extends ProtocolTableAccessor {

    /**
     * Default constructor.
     */
    public Protocol() {
        super();
    }

    /**
     * Wrapper for the superclass constructor.
     *
     * @param aParams   HashMap with the parameters.
     */
    public Protocol(HashMap aParams) {
        super(aParams);
    }

    /**
     * This constructor reads the protocol from a resultset. The ResultSet should be positioned such that
     * a single row can be read directly (i.e., without calling the 'next()' method on the ResultSet). <br />
     * The columns should be in this order: <br />
     * Column 1: protocol ID <br />
     * Column 2: name of the protocol <br />
     * Column 3: the description for the protocol <br />
     * Column 4: username <br />
     * Column 5: creationdate <br />
     * Column 6: modificationdate.
     *
     * @param   aRS ResultSet to read the data from.
     * @exception java.sql.SQLException    when reading the ResultSet failed.
     */
    public Protocol(ResultSet aRS) throws SQLException {
        iProtocolid = aRS.getLong(1);
        iName = aRS.getString(2);
        iDescription = aRS.getString(3);
        iUsername = aRS.getString(4);
        iCreationdate = (java.sql.Timestamp)aRS.getObject(5);
        iModificationdate = (java.sql.Timestamp)aRS.getObject(6);
    }

    /**
     * This methods reads all protocols from the Protocol table.
     *
     * @param aConn Connection to read the protocols from.
     * @return  Protocol[] with the protocols in the 'Protocol' table.
     * @throws SQLException when the retrieving of the protocols went wrong.
     */
    public static Protocol[] getAllProtocols(Connection aConn) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from protocol");
        ResultSet rs = prep.executeQuery();
        Vector v = new Vector();
        while(rs.next()) {
            v.add(new Protocol(rs));
        }
        rs.close();
        prep.close();
        Protocol[] lProtocols = new Protocol[v.size()];
        v.toArray(lProtocols);

        return lProtocols;
    }


    /**
     * Returns a String representation for this Protocol.
     *
     * @return  String  with the String representation for this Protocol.
     */
    public String toString() {
        return this.iName;
    }

}
